How to create an Android menu?

Ok, so adding a menu that pops up from the bottom when the menu button is clicked is very common and quite easy to do.

Note: This assumes you have the Android SDK, Emulator, and Eclipse all working already.

Step 1 – Create your Android project

  1. In Eclipse, select File | New Project | Android | Android Project.
  2. Give your project a Name.
    I named this project “HelloAll”.
  3. Select the Build Target (the minimum version of Android).
    I selected Android 2.2.
  4. Enter a Package name.
    Package name is like a namespace, it can be anything you want, but you should actually choose a name as carefully as you choose and the name of an object.  I named the package this: org.rhyous.
  5. Click Finish.

Your project is now created.

Step 2 – Add an XML file for the menu

  1. Expand the res directory in your project.
  2. Right-click on the layout folder and choose New | Other.
  3. Choose XML | XML file and click Next.
  4. Name the file.
    I named my file menu.xml.
  5. Click Finish.
  6. Add the following text into your menu:
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        id="@+id/menu_item_1" android:title="@string/menu_1"/>
        id="@+id/menu_item_2" android:title="@string/menu_2"/>
        <item android:id="@+id/menu_item_3" android:title="@string/menu_3"/>
    </menu>
    

Step 3 – Add the strings for the menu items

  1. Expand the res\values directory in your project.
  2. Open the strings.xml.
  3. Add strings for each menu item.
    Make sure you use the same id strings you used in the menu.xml for the title of each menu item.
    Your strings.xml should now look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, HelloAllActivity!</string>
        <string name="app_name">HelloAll</string>
        <string name="menu_1">Menu 1</string>
        <string name="menu_2">Menu 2</string>
        <string name="menu_3">Menu 3</string>
    </resources>
    

You now have a menu and strings for each menu item.

Step 4 – Overload onCreateOptionsMenu

  1. Open your Activity.
    Mine is src\org.rhyous\HelloAllActivity.java.
    It should look like this:

    package org.rhyous;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class HelloAllActivity extends Activity {
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle inSavedInstanceState) {
    		super.onCreate(inSavedInstanceState);
    		setContentView(R.layout.main);
    	}
    }
    
  2. Add code to override onCreateOptionsMenu and add code to inflate the menu.
    	@Override
    	public boolean onCreateOptionsMenu(Menu inMenu) {
    		super.onCreateOptionsMenu(inMenu);
    		getMenuInflater().inflate(R.layout.menu, inMenu);
    		return true;
    	}
    

You can now build your application and test that the menu pops up. However, the menu doesn’t do anything yet.

Step 5 – Overload onCreateOptionsMenu

  1. Add code to override onOptionsItemSelected and add code to inflate the menu.
  2. Use a switch statement with the inItem.getItemId() function to perform the appropriate action for each menu item.
    	@Override
    	public boolean onOptionsItemSelected(MenuItem inItem) {
    		switch (inItem.getItemId()) {
    		case R.id.menu_item_1:
    			// Do something here
    			return true;
    		case R.id.menu_item_2:
    			// Do something here
    			return true;
    		default:
    			// Should never get here
    			return false;
    		}
    

Based on the item clicked, the appropriate code will run.

Hope you enjoyed this simple Android development example.

Leave a Reply

How to post code in comments?